#include "utilAPI.h"

extern FrameAPI gl_CurrentFrameAPI;

int recept(int TimeOut)
{
    char nextCarEscaped = 0;
    unsigned char curChar = '\0';
    int iIndex = 0;
    int iFrameLength = 0;
    unsigned char bufferRecept[MAX_PAYLOAD];
    while (TimeOut > 0)
    {
        if(ReceptionUSART1((char*)&curChar, TIMEOUT_RCV_CAR) == CAR_RECU)
        {
            if(curChar == (unsigned)FRAME_DELIMITER)
            {
                iIndex = 0;
                iFrameLength = 0;
            }
            else if(curChar == (unsigned)ESCAPE)
            {
                nextCarEscaped = 1;
            }
            else
            {
                if(nextCarEscaped)
                {
                    curChar = curChar^0x20;
                    nextCarEscaped = 0;
                }
                bufferRecept[iIndex] = curChar;
                if(iIndex == 1)
                {
                    iFrameLength = bufferRecept[0]*256 + bufferRecept[1];
                }
                if(iIndex == bufferRecept[0]*256 + bufferRecept[1] + 2)
                {
                    bufferRecept[iIndex + 1] = '\0';
                    parseApi(bufferRecept);
                    return TimeOut;
                }
                iIndex ++;
            }
        }
        else
        {
            TimeOut --;
        }
    }
    return TimeOut;
}

char waitForTransmitStatusPacket(void)
{
    if(recept(5) == TIMEOUT_WTD)
    {   // Tx status not received
        return TIMEOUT;
    }
    if((unsigned char)gl_CurrentFrameAPI.content.frame.frameType == API_CMD_TX_STATUS)
    {   // Tx status received
        return gl_CurrentFrameAPI.content.frame.api.transmitStatus.deliveryStatus;
    }
    else
    {   // Expecting Tx Status but an other packet received
        return UNEXPECTING_FRAME_RECEIVED;
    }
}

void parseApi(unsigned char *buff)
{
    int iCpt = 0;
    int FrameSize = 0;
    gl_CurrentFrameAPI.length_MSB = buff[0];
    gl_CurrentFrameAPI.length_LSB = buff[1];
    FrameSize = gl_CurrentFrameAPI.length_MSB*256 + gl_CurrentFrameAPI.length_LSB;
    for(iCpt = 0 ; iCpt < FrameSize ; iCpt ++)
    {
        gl_CurrentFrameAPI.content.data[iCpt] = buff[iCpt + 2];
    }
    gl_CurrentFrameAPI.content.frame.checksum = buff[iCpt + 2];
}

void displayAPIFrame()
{
//    int iCpt = 0;
//    int FrameSize = fApi.length_MSB*256 + fApi.length_LSB;
//    printf("\n(%x) >> ",(unsigned char)fApi.content.frame.frameType);
//    switch((unsigned char)fApi.content.frame.frameType)
//    {
//        case API_CMD_AT_CMD:
//        printf("Commande AT : ");
//        break;
//        case API_CMD_AT_CMD_PARAMETER:
//        printf("Commande AT avec parametres : ");
//        break;
//        case API_CMD_AT_CMD_RESPONSE:
//        printf("Reponse a une commande AT : ");
//        break;
//        case API_CMD_CREATE_SRC_ROUTE:
//        printf("Commande de creation d'une route source : ");
//        break;
//        case API_CMD_MODEM_STATUS:
//        printf("Status du modem : ");
//        break;
//        case API_CMD_RECV_PACKET:
//        printf("Reception d'un paquet : ");
//        break;
//        case API_CMD_REMOTE_CMD:
//        printf("Commande AT a distance : ");
//        break;
//        case API_CMD_TX_REQUEST:
//        printf("Demande d'envoi de donnees : ");
//        break;
//        case API_CMD_TX_STATUS:
//        printf("Statut de l'envoi de donn�es : ");
//        break;
//        default:
//        printf("Commande non definie : ");
//        break;
//    }
//    putchar('\n');
//    printf("%x [%x %x] ", FRAME_DELIMITER, (unsigned char)fApi.length_MSB, (unsigned char)fApi.length_LSB);
//    for(iCpt = 0 ; iCpt < FrameSize ; iCpt ++)
//    {
//        printf("%x ", (unsigned char)fApi.content.data[iCpt]);
//    }
//    printf("Checksum : %x \n", (unsigned char)fApi.content.frame.checksum);
}

char stringCopy(unsigned char* src, unsigned char* dest)
{
    int iCpt = 0;
    while(iCpt < NODE_ID_MAX_LENGTH)
    {
        if(src[iCpt] == '\0')
        {
            dest[iCpt] = '\0';
            return iCpt;
        }
        dest[iCpt] = src[iCpt];
        iCpt ++;
    }
    dest[iCpt] = '\0';
    return iCpt;
}

int stringCompare(unsigned char* str1, unsigned char* str2)
{
    int iCpt = 0;
    while(iCpt < NODE_ID_MAX_LENGTH)
    {
        if((str1[iCpt] == '\0')&&(str2[iCpt] == '\0'))
        {
            return 1;
        }
        else if (str1[iCpt] != str2[iCpt])
        {
            return 0;
        }
        iCpt ++;
    }
    return 1;
}
